package world.criver.h50529.controller;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import world.criver.h50529.application.appStartInform;
import world.criver.h50529.data.daControllers;
import world.criver.h50529.data.daData;
import world.criver.h50529.data.daFxml;
import world.criver.h50529.function.funFiles;
import world.criver.h50529.function.funStageMove;
import world.criver.h50529.function.funTray;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;

public class conMaster implements Initializable {

    public BorderPane box;
    public Label menu_item_1;
    public Label menu_item_2;
    public Label menu_item_3;
    public Label menu_item_4;
    public Label menu_item_5;

    private funStageMove funStageMove=new funStageMove();

    public void onMenuItem1() {
        setCenter(daFxml.page1);
        updateMenuItemBg(1);
    }

    public void onMenuItem2() {
        setCenter(daFxml.page2);
        updateMenuItemBg(2);
        daControllers.page2.updateList();
    }

    public void onMenuItem3() {
        setCenter(daFxml.page3);
        updateMenuItemBg(3);
        daControllers.page3.updateList();
    }

    public void onMenuItem4() {
        setCenter(daFxml.page4);
        updateMenuItemBg(4);
    }
    public void onMenuItem5() {
        setCenter(daFxml.page5);
        updateMenuItemBg(5);
        daControllers.page5.updateList();
    }

    public void onMenuItemExit() {
        new funTray().remove();
        funFiles ff=new funFiles();
        ff.saveEvents();
        ff.saveListens();
        ff.saveTransmit();
        System.exit(0);
    }

    public void onWinMin() {
        daData.master.setIconified(true);
    }

    public void onWinClose() {
        daData.master.hide();
    }

    public void onTitlePress(MouseEvent e) {
        funStageMove.onPress(e.getScreenX(),e.getScreenY(),daData.master);
    }

    public void onTitleRelease(MouseEvent e) {
        funStageMove.onRelease();
    }

    public void onTitleDrag(MouseEvent e) {
        funStageMove.onDrag(e.getScreenX(),e.getScreenY(),daData.master);
    }
    public void setCenter(Node node){
        Platform.runLater(()->{
            box.setCenter(node);
        });
    }
    public void updateMenuItemBg(int num){
        menu_item_1.getStyleClass().clear();
        menu_item_2.getStyleClass().clear();
        menu_item_3.getStyleClass().clear();
        menu_item_4.getStyleClass().clear();
        menu_item_5.getStyleClass().clear();
        menu_item_1.getStyleClass().add("menu_item"+(num==1?"_selected":""));
        menu_item_2.getStyleClass().add("menu_item"+(num==2?"_selected":""));
        menu_item_3.getStyleClass().add("menu_item"+(num==3?"_selected":""));
        menu_item_4.getStyleClass().add("menu_item"+(num==4?"_selected":""));
        menu_item_5.getStyleClass().add("menu_item"+(num==5?"_selected":""));
    }
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        Platform.runLater(()->{
            onMenuItem1();
            testProcess();
        });
    }
    private void testProcess(){
        try{
            ProcessBuilder pb=new ProcessBuilder("tasklist");
            Process p=pb.start();
            BufferedReader re=new BufferedReader(new InputStreamReader(p.getInputStream()));
            String l;
            int num=0;
            while((l=re.readLine())!=null){
                if(l.contains("TSTIO.exe"))num++;
            }
            re.close();
            if(num>1){
                new appStartInform("已启动进程",false).start(new Stage());
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        System.exit(0);
                    }
                } ,3000);
            }else{
                daControllers.page1.bu_start.fire();
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}